<?php

/**
 * Creating HTML to visualizise data about subdivisions
 * 
 * @author Aurelijus Banelis
 */
class SubdivisionsDesign {
      
    /**
     * @param array $dates           [date]=>boolean is predicted
     * @param array $subdivisions   [code]=>name
     * @param array $time_groups    [subdivision][date]=>TimeGroup
     */
    public static function table_quantity($dates, $subdivisions, $time_groups) {
        ?><div class="data_table"><table class="data_table" border="1">
            <?php
                self::table_header($dates, 'Padalinys | Laikotarpis');
                foreach ($time_groups as $subdivsion_code=>$row) {
                    $subdivision_name = htmlspecialchars($subdivisions[$subdivsion_code]);
                    echo "<tr><td class='head_column'>$subdivision_name</td>";
                    foreach ($row as $date=>$time_group) {
                        $received = $time_group->sum_received();
                        $previous = $time_group->sum_previous();
                        $administrated = $time_group->sum_administrated();
                        $predicted = $time_group->predicted ? ' class="predicted"' : '';
                        echo "<td $predicted>";
                            echo "<span class='received'>$received</span><br/>";
                            echo "<span class='previous'>$previous</span><br/>";
                            echo "<span class='addministrated'>$administrated</span><br/>";
                        echo '</td>';
                    }
                    echo '</tr>';
                }
            ?>
        </table></div>
        <?php
    }
    
    /**
     * @param array $dates           [date]=>boolean is predicted
     * @param array $subdivisions   [code]=>name
     * @param array $time_groups    [subdivision][date]=>TimeGroup
     */
    public static function table_time($dates, $subdivisions, $time_groups) {
        ?><div class="data_table"><table class="data_table" border="1">
            <?php
                self::table_header($dates, 'Padalinys | Laikotarpis');
            
                foreach ($time_groups as $subdivsion_code=>$row) {
                    $subdivision_name = htmlspecialchars($subdivisions[$subdivsion_code]);
                    echo "<tr><td class='head_column'>$subdivision_name</td>";
                    foreach ($row as $date=>$time_group) {
                        $time = intval($time_group->sum_time_used());
                        $predicted = $time_group->predicted ? 'predicted' : '';
                        echo "<td class='time $predicted'>$time</td>";
                    }
                    echo '</tr>';
                }
            ?>
        </table></div>
        <?php
    }
   
    private static function table_header($dates, $informationCell) {
        $informationCell = htmlspecialchars($informationCell);
        ?><tr class="head_row"><?php
            echo "<td class='corner'>$informationCell</td>";
            foreach ($dates as $date=>$is_predicted) {
                $time = htmlspecialchars($date);
                $date = str_replace('-', ' - ', $date);
                $class = $is_predicted ? 'class="predicted"' : '';
                echo "<td $class>$date</td>";
            }
        ?></tr><?php
    }
    
    public static function is_quantity($dates, $is_names, $is_time_groups, $show_time = false) {
        ?><div class="data_table"><table class="data_table" border="1">
            <?php
                self::table_header($dates, 'IS | Laikotarpis');
                
                foreach ($is_time_groups as $is_code=>$row) {
                    $is_name = htmlspecialchars($is_names[$is_code]);
                    echo "<tr><td class='head_column'>$is_name</td>";
                    foreach ($row as $date=>$is_time_group) {
                        $used = $is_time_group->sum_n;
                        $time = $is_time_group->sum_time;
                        $predicted = $is_time_group->predicted ? ' class="predicted"' : '';
                        echo "<td $predicted>";
                            if ($show_time) {
                                echo "<span class='time'>$time</span>";
                            } else {
                                echo "<span class='received'>$used</span>";
                            }
                        echo '</td>';
                    }
                    echo '</tr>';
                }
            ?>
        </table></div>
        <?php
    }
}